﻿using Common;
using SqliteDataAccess.SqliteDataManagerTableAdapters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqliteDataAccess;

namespace Business
{
    public class AktivasiBusiness : BaseBusiness
    {

        public override int Load()
        {
            ActivationTableAdapter ta = new ActivationTableAdapter();
            return ta.Fill(LppbopSqlite.Activation);
        }

        public SqliteDataManager.ActivationDataTable LoadData()
        {
            ActivationTableAdapter ta = new ActivationTableAdapter();
            SqliteDataManager.ActivationDataTable dt = ta.GetData();
            return dt;
        }

        public int Save(string mac, long serial, long activation)
        {
            ActivationTableAdapter ta = new ActivationTableAdapter();
            SqliteDataManager.ActivationDataTable dt = ta.GetData();
            SqliteDataManager.ActivationRow row = null;
            if (dt.Count == 0)
            {
                row = dt.NewActivationRow();
                row.activation_code = activation;
                row.product_number = serial;
                row.mac_address = mac;
                row.valid = true;
                dt.AddActivationRow(row);
            }
            else
            {
                row = dt[0];
                row.activation_code = activation;
                row.product_number = serial;
                row.mac_address = mac;
                row.valid = true;
            }

            return ta.Update(dt);
        }

        public string GetRegisteredMacAddress()
        {
            ActivationTableAdapter ta = new ActivationTableAdapter();
            SqliteDataManager.ActivationDataTable dt = ta.GetData();
            if (dt.Count == 0)
                return string.Empty;
            else return dt[0].mac_address;
        }

        public bool ValidateActivationCode(long serialNumber, long activationCode)
        {
            string mac = GuardManager.GetMacAddress();
            if (GuardManager.IsValid(mac, serialNumber, activationCode))
            {
                Save(mac, serialNumber, activationCode);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ValidateActivationCode(string mac, long serialNumber, long activationCode)
        {
            return GuardManager.IsValid(mac, serialNumber, activationCode);
        }

        public override int Save()
        {
            throw new NotImplementedException();
        }

        public bool IsActivated()
        {
            SqliteDataManager.ActivationDataTable dt = LoadData();
            if (dt.Rows.Count == 0)
                return false;
            else
            {
                SqliteDataManager.ActivationRow row = (SqliteDataManager.ActivationRow)dt.Rows[0];
                bool valid = ValidateActivationCode(row.mac_address, Convert.ToInt64(row.product_number), Convert.ToInt64(row.activation_code));
                row.valid = valid;
                new ActivationTableAdapter().Update(dt);

                return valid;
            }
        }
    }
}
